let { addrequirement ,addOperateLog} = require("../../../api/programs/add");
let { getrequirement } = require("../../../api/programs/Detail");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    requirement: {"xmbh":"",//项目编号
    "xqlb":"",//需求类别
    "xqx":"",//需求项
    "xqmx":"",//需求内容
   
    "tname":""//添加人
    }, 
    showDialog: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      "requirement.xmbh":options.xmbh,
      "requirement.tname": options.tname,
      bm:options.bm,
      tname:options.tname,
      xmmc:options.xmmc
    });
    (options.xqID !== null) &
    (options.xqID !== "") &
    (options.xqID !== undefined)
      ? this.getrequirement()
      : "";
   
  },
  getrequirement(){
    getrequirement(this.data.requirement.xmbh).then((res) => {
      let newData = { ...this.data.requirement };
      this.setData({
        requirement: { ...newData, ...res.data[0] },
     
      });
   
    });
  },
  onPush() {
    try {
   
      // for (const key in this.data.requirement) {
      //   if (this.data.requirement[key] === "") {
      //     wx.showToast({
      //       title: "请填写必填项",
      //       icon: "error",
      //       duration: 2000, //持续的时间
      //     });
      //     return;
      //   }
      // }

      this.Editrequirement();
    } catch (error) {
      console.log(error);
    }
  },
  Editrequirement() {
    let time = new Date().getTime();
    //将时间戳格式转换成年月日时分秒
    let date = new Date(time);
    let bbTime =`${date.getFullYear()}-${
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1
    }-${date.getDate() < 10 ? "0" + date.getDate() : date.getDate()}`;
    let tjTime = `${
      date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
    }:${date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()}:${
      date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
    }`;
    this.setData({
      "requirement.tjsj": `${bbTime} ${tjTime}`,
    });
    addrequirement(this.data.requirement).then((res) => {
      wx.showToast({
        title: "意向需求填报完成",
        icon: "none",
      });
      setTimeout(() => {
        this.setData({ showDialog: true });
        wx.redirectTo({
          url: `/pages/programs/detail/detail?xmbh=${this.data.requirement.xmbh}`,
        })
      }, 1000);
    }) .then(() => {
      addOperateLog({
        type: 2,
        khbh: this.data.requirement.khbh,
        xmbh: this.data.requirement.xmbh,
        xmmc: this.data.xmmc,
        bm: this.data.bm,
        tname: this.data.tname,
        tjsj: `${bbTime} ${tjTime}`,
      });
    });
  },
  inputValue(e) {
    let param = e.currentTarget.dataset.name; //字段名
    let value = e.detail; //值

    let key = `requirement.${param}`;
    // const msg=`checkData.${param}`;
   
    this.setData({
      [key]: value,
      // [msg]:'' //清空错误提示
    });
  },
  

 
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
